﻿using SqlSugar;
using web_api_demo101.Entities;
using web_api_demo101.Repositories.Assist;

namespace web_api_demo101.Repositories.Impl;


public class StudentRepository : IStudentRepository
{
    private readonly SqlSugarClient dbClient;
    public StudentRepository(SqlSugarClient dbClient)
    {
        this.dbClient = dbClient;
    }

    public int Add(Student stu)
    {
        stu.Created = DateTime.Now;
        stu.Updated = DateTime.Now; // 避免插入一个"1900-01-01T00:00:00"的默认值
        return dbClient.Insertable(stu).ExecuteCommand();
    }

    public PageData<Student> PaginationQuery(int page, int pagesize)
    {
        int totalCount = 0;
        List<Student> stuList = dbClient.Queryable<Student>().ToPageList(page, pagesize, ref totalCount);
        return new PageData<Student>
        {
            total = totalCount,
            data = stuList
        };
    }

    public int Update(Student stu)
    {
        stu.Updated = DateTime.Now;
        return dbClient.Updateable(stu).ExecuteCommand();
    }

    public Student GetById(int id)
    {
        return dbClient.Queryable<Student>().First(it => it.Id == id);
    }
}

